package com.patentbackend.patentmanage.controller;

import com.patentbackend.patentmanage.dto.LegalDocumentDTO;
import com.patentbackend.patentmanage.entity.LegalDocument;
import com.patentbackend.patentmanage.service.LegalDocumentService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;

@RestController
@RequestMapping("/api/legal-docs")
public class LegalDocumentController {

    private final LegalDocumentService legalDocumentService;

    public LegalDocumentController(LegalDocumentService legalDocumentService) {
        this.legalDocumentService = legalDocumentService;
    }

    @GetMapping
    public ResponseEntity<Page<LegalDocument>> getAllLegalDocuments(Pageable pageable) {
        return ResponseEntity.ok(legalDocumentService.findAll(pageable));
    }

    @GetMapping("/{id}")
    public ResponseEntity<LegalDocument> getLegalDocumentById(@PathVariable Long id) {
        return legalDocumentService.findById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<LegalDocument> createLegalDocument(
            @Valid @RequestBody LegalDocumentDTO documentDTO
    ) {
        LegalDocument document = legalDocumentService.create(documentDTO);
        return ResponseEntity.status(HttpStatus.CREATED).body(document);
    }

    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<LegalDocument> updateLegalDocument(
            @PathVariable Long id,
            @Valid @RequestBody LegalDocumentDTO documentDTO
    ) {
        if (!legalDocumentService.existsById(id)) {
            return ResponseEntity.notFound().build();
        }
        LegalDocument updatedDocument = legalDocumentService.update(id, documentDTO);
        return ResponseEntity.ok(updatedDocument);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<Void> deleteLegalDocument(@PathVariable Long id) {
        if (!legalDocumentService.existsById(id)) {
            return ResponseEntity.notFound().build();
        }
        legalDocumentService.delete(id);
        return ResponseEntity.noContent().build();
    }

    @PostMapping("/{id}/upload-file")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<?> uploadFile(
            @PathVariable Long id,
            @RequestParam("file") MultipartFile file
    ) throws IOException {
        if (!legalDocumentService.existsById(id)) {
            return ResponseEntity.notFound().build();
        }
        String filePath = legalDocumentService.uploadFile(id, file);
        return ResponseEntity.ok(filePath);
    }
}